<?php

class UserController extends BaseController
{
    public function profileAction()
    {
        $id = $this->getParam('login');
        $user = User::getByParams(array('login' => $id));
        if (!$user) {
            Url::redirect('/');
        }

        $this->view->assign('nav', array('Профіль користувача ' . $user->login => ''));

        $this->view->assign('profile', $user);
        $this->view->display('user.profile.tpl');
        //print 'default action of ProjectsController';
    }

    public function editAction()
    {
        $id = $this->getParam('id');
        $project = Project::getById(intval($id));
        if (!$project) {
            Url::redirect('/projects/');
        }
        $this->view->assign('nav', array('Список проектів' => '/projects/', 'Редагування проекту ' . $project->name => ''));

        $this->view->assign('project', $project);
        $this->view->display('projects.edit.tpl');
    }

    public function changeavatarAction()
    {
        $user = Session::Singleton()->user;

        if (!$user) {
            Url::redirect('/');
        }

        $this->view->assign('profile', $user);
        $this->view->display('user.changeavatar.tpl');
    }

    public function saveavatarAction()
    {
        $user = Session::Singleton()->user;
        if (!empty($_GET['image'])) {
            $img = SITE_DIR . $_GET['image'];

            if ($user == null) {
                echo 'Error';
                return;
            }
            if (file_exists($img)) {
                $size = getimagesize($img);

                switch ($size['mime']) {
                    case "image/gif":
                        $img_r = imagecreatefromgif($img);
                        break;
                    case "image/jpeg":
                        $img_r = imagecreatefromjpeg($img);
                        break;
                    case "image/png":
                        $img_r = imagecreatefrompng($img);
                        break;
                } 
                $targ_w = $targ_h = 32;
                $jpeg_quality = 90;
                $dst_r = imagecreatetruecolor($targ_w, $targ_h);

                $white = imagecolorallocate($dst_r, 255, 255, 255);
                imagefill($dst_r, 0, 0, $white);
                imagecopyresampled($dst_r,$img_r,0,0,$_GET['x'],$_GET['y'],
                                    $targ_w,$targ_h,$_GET['w'],$_GET['h']);

                $targetPath = UPLOAD_DIR . '/';
                $fileName = Guid::newGuid() . '.jpg';
                $targetFile =  str_replace('//','/',$targetPath) . $fileName;

                imagejpeg($dst_r, $targetFile, $jpeg_quality);

                $user->avatar = '/tmp/upload/' . $fileName;
                $user->save();
            }
        }
        Url::redirect('/users/' . $user->login);
    }

    public function uploadavatarAction()
    {
        if (!empty($_FILES)) {
            $fileParts  = pathinfo($_FILES['Filedata']['name']);

            $tempFile = $_FILES['Filedata']['tmp_name'];
            $targetPath = UPLOAD_DIR . '/';
            $fileName = Guid::newGuid() . '.' . $fileParts['extension'];
            $targetFile =  str_replace('//','/',$targetPath) . $fileName;
            
            // $fileTypes  = str_replace('*.','',$_REQUEST['fileext']);
            // $fileTypes  = str_replace(';','|',$fileTypes);
            // $typesArray = split('\|',$fileTypes);
            
            // if (in_array($fileParts['extension'],$typesArray)) {
                // Uncomment the following line if you want to make the directory if it doesn't exist
                // mkdir(str_replace('//','/',$targetPath), 0755, true);
                
                move_uploaded_file($tempFile, $targetFile);
                echo $fileName;
            // } else {
            // 	echo 'Invalid file type.';
            // }
        }
    }


    public function changephotoAction()
    {
        $user = Session::Singleton()->user;

        if (!$user) {
            Url::redirect('/');
        }

        $this->view->assign('profile', $user);
        $this->view->display('user.changephoto.tpl');
    }

    public function savephotoAction()
    {
        $user = Session::Singleton()->user;
        if (!empty($_GET['image'])) {
            $img = SITE_DIR . $_GET['image'];

            if ($user == null) {
                echo 'Error';
                return;
            }
            if (file_exists($img)) {
                $size = getimagesize($img);

                switch ($size['mime']) {
                    case "image/gif":
                        $img_r = imagecreatefromgif($img);
                        break;
                    case "image/jpeg":
                        $img_r = imagecreatefromjpeg($img);
                        break;
                    case "image/png":
                        $img_r = imagecreatefrompng($img);
                        break;
                } 
                $targ_w = 220;
                $targ_h = 200;
                $jpeg_quality = 90;
                $dst_r = imagecreatetruecolor($targ_w, $targ_h);

                $white = imagecolorallocate($dst_r, 255, 255, 255);
                imagefill($dst_r, 0, 0, $white);
                imagecopyresampled($dst_r,$img_r,0,0,$_GET['x'],$_GET['y'],
                                    $targ_w,$targ_h,$_GET['w'],$_GET['h']);

                $targetPath = UPLOAD_DIR . '/';
                $fileName = Guid::newGuid() . '.jpg';
                $targetFile =  str_replace('//','/',$targetPath) . $fileName;

                imagejpeg($dst_r, $targetFile, $jpeg_quality);

                $user->photo = '/tmp/upload/' . $fileName;
                $user->save();
            }
        }
        Url::redirect('/users/' . $user->login);
    }

    public function uploadphotoAction()
    {
        if (!empty($_FILES)) {
            $fileParts  = pathinfo($_FILES['Filedata']['name']);

            $tempFile = $_FILES['Filedata']['tmp_name'];
            $targetPath = UPLOAD_DIR . '/';
            $fileName = Guid::newGuid() . '.' . $fileParts['extension'];
            $targetFile =  str_replace('//','/',$targetPath) . $fileName;
            
            // $fileTypes  = str_replace('*.','',$_REQUEST['fileext']);
            // $fileTypes  = str_replace(';','|',$fileTypes);
            // $typesArray = split('\|',$fileTypes);
            
            // if (in_array($fileParts['extension'],$typesArray)) {
                // Uncomment the following line if you want to make the directory if it doesn't exist
                // mkdir(str_replace('//','/',$targetPath), 0755, true);
                
                move_uploaded_file($tempFile, $targetFile);
                echo $fileName;
            // } else {
            // 	echo 'Invalid file type.';
            // }
        }
    }

    public function viewAction()
    {
        //$this->view->display('examples/example1.tpl');
    }
}